package bean;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import util.JPAUtil;
import modelo.Modelo;

@ManagedBean
@SessionScoped
public class ModeloBean {
	
	Modelo modelo = new Modelo();
	List<Modelo> modelos;
	
	@SuppressWarnings("unchecked")
	public List<Modelo> getModelos() {
		if(modelos==null){
			EntityManager em = JPAUtil.getEntityManager();
			Query q = em.createQuery("select modelo from Modelo modelo",
			Modelo.class);
			this.modelos = q.getResultList();
			em.close();
		}
		return modelos;
	}

	public void salva(){
		EntityManager em = JPAUtil.getEntityManager();
		em.getTransaction().begin();
		em.persist(modelo);
		em.getTransaction().commit();
		em.close();
	}

}
